home *** CD-ROM | disk | FTP | other *** search
- Path: walters.East.Sun.COM!usenet
- From: Henry Wong <henry.wong@sun.com>
- Newsgroups: comp.lang.pascal.misc,comp.lang.c++,comp.lang.c,comp.lang.pascal.borland
- Subject: Re: Tough FACTORIAL math problem...
- Date: Fri, 16 Feb 1996 14:43:01 -0500
- Organization: Sun Microsystems, Inc.
- Message-ID: <3124DE45.CEB@sun.com>
- References: <4fr8be$ass@news.iconn.net> <31224679.6193@born.com> <4fv74c$chq@gatekeeper.alcatel.no>
- NNTP-Posting-Host: enchanter.east.sun.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0b6a (X11; I; SunOS 5.4 sun4m)
- CC: sven.pran@alcatel.no, clelaj@born.com, thecrow@iconn.net
-
- > >The Crow wrote:
- > >>
- > >> Here is what I am trying to do, I want to find the last NON-ZERO
- > >> digit of a given factorial. For instance,
- > >>
- > >> 5! = 120
- > >>
- > >> so the last non-zero digit is 2. I want to be able to do this
- > >> up to 1000. Problem is, no matter how huge of a data-type you use,
- > >> there isn't any way for the computer to handle 1000!, it is
- > >> however possible to find the last non-zero digit somehow. One
- > >> thing I have tried is as I went through multiplying the series of
- > >> numbers in the factorial (5 * 4 * 3 * 2) I would remove all the
- > >> trailing ZEROS, I got this to work up to 789, but it wont
- > >> work with 1000 and i am not really sure why. If anyone has a
- > >> clue how I can do this let me know.
- > >
-
- Believe it or not, you do not need a brute force calculation to find
- the answer. Simply look at it another way...
-
- How do you add a zero to a number? answer, multiply it by 10, which
- means that broken down to primes, multiple it by 2 and 5.
-
- Well, you get a two everytime you multiply an even number, and you
- get a five every time it is divisiable by 5. Since, there is more even
- numbers than every 5, then you can say "the number of zeros at the
- end is the number of fives, when the number is broken down to primes"
-
- Obvious, you get a 5 at every 5, so there are (1000/5) = 200 obvious
- fives. You also get a extra 5 at every 25 so there are (1000/25) = 40
- fives from that. Well, you get the pattern...
-
- (1000/5) = 200
- (1000/25) = 40
- (1000/125) = 8
- (1000/625) = 1
- ---------------
- 249 fives or 249 trailing zeros
-
- This will of course break when it hits 3125! which can be solved
- by adding another term.
-